package com.aratana.converter;

import java.lang.reflect.ParameterizedType;

public abstract class Converter<From, To> {

	private final Class<From> fromClass;
	private final Class<To> toClass;

	@SuppressWarnings("unchecked")
	public Converter() {
		try {
			fromClass = (Class<From>) Class.forName(((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].toString().replaceAll("class ", ""));
			toClass = (Class<To>) Class.forName(((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1].toString().replaceAll("class ", ""));
		} catch (final Exception e) {
			throw new RuntimeException("Problems on init converter.", e);
		}
	}

	public abstract To convert(From from) throws Exception;

	public final Class<From> getFromClass() {
		return fromClass;
	}

	public final Class<To> getToClass() {
		return toClass;
	}
}
